/**  

* <p>Title: SenderController.java</p>  

* <p>Description: </p>  

* <p>Copyright: Copyright (c) 2017</p>  

* <p>Company: npu</p>  

* @author zhangxuhao  

* @date Nov 18, 2019  

* @version 1.0  

*/
package cdms.web;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import java.util.Date;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cdms.db.DeliveryOrderRepository;
import cdms.db.SenderRepository;
import cdms.domain.DeliveryOrder;
import cdms.domain.Sender;

/**
 * 
 * <p>
 * Title: SenderController
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author zhangxuhao JiHongru
 * 
 * @date Nov 18, 2019
 * 
 */
@Controller
@RequestMapping("/sender")
public class SenderController {

	@Autowired
	private SenderRepository senderRepository;

	@Autowired
	private DeliveryOrderRepository deliveryOrderRepository;

	/**
	 * 
	 * 
	 * <p>
	 * Title: showSenderLoginForm
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login", method = GET)
	public String showSenderLoginForm(HttpServletRequest request, HttpSession session, Model model) {
		if (session != null) {
			if (session.getAttribute("senderId") != null) {
				session.removeAttribute("orderIng");
				DeliveryOrder deliveryOrder = deliveryOrderRepository
						.findOneBySenderId((Long) session.getAttribute("senderId"));
				session.setAttribute("orderIng", deliveryOrder);
				return "redirect:/";
			} else {
				session.invalidate();
				Cookie cookies[] = request.getCookies();
				if (cookies != null) {
					for (int i = 0; i < cookies.length; i++) {
						if (cookies[i].getName().equals("senderUserName")) {
							model.addAttribute("senderUserName", cookies[i].getValue());
							break;
						}
					}
				}
				return "sender/senderLoginForm";
			}
		}
		return "sender/senderLoginForm";
	}

	/**
	 * 
	 * 
	 * <p>
	 * Title: processSenderLogin
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param userName
	 * @param password
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login", method = POST)
	public String processSenderLogin(@RequestParam(value = "userName", defaultValue = "") String userName,
			@RequestParam(value = "password", defaultValue = "") String password, HttpSession session, Model model,
			HttpServletResponse response) {
		session.removeAttribute("sender");
		session.removeAttribute("manager");
		Sender sender = senderRepository.findOneByUsernameAndPassWord(userName, password);
		if (sender != null) {
			Cookie cookie = new Cookie("senderUserName", userName);
			cookie.setMaxAge(30 * 24 * 60 * 60);
			response.addCookie(cookie);
			senderRepository.updateOnline(true, userName);
			session.setAttribute("sender", sender);
			session.setAttribute("senderId", sender.getId());
			session.removeAttribute("orderIng");
			DeliveryOrder deliveryOrder = deliveryOrderRepository.findOneBySenderId(sender.getId());
			session.setAttribute("orderIng", deliveryOrder);
			return "redirect:/";
		} else {
			model.addAttribute("msg", "用户名或密码错误！");
			return "sender/senderLoginForm";
		}

	}

	/**
	 * sender logout
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/senderLogout", method = GET)
	public String logout(HttpSession session) {
		Sender sender = (Sender) session.getAttribute("sender");
		if (sender != null)
			senderRepository.updateOnline(false, sender.getUserName());
		session.removeAttribute("sender");
		session.invalidate();
		return "redirect:/";
	}

	/**
	 * 
	 * 
	 * <p>
	 * Title: getChange
	 * </p>
	 * 
	 * <p>
	 * Description: return changeInformation page
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/changeInformation", method = GET)
	public String showChange(Model model, HttpSession session) {
		Sender sender = (Sender) session.getAttribute("sender");

		if (sender != null) {
			model.addAttribute("sender", sender);
			return "sender/changeInformation";
		} else {
			return "redirect:/";
		}

	}

	/**
	 * 
	 * 
	 * <p>
	 * Title: processedit
	 * </p>
	 * 
	 * <p>
	 * Description: change information
	 * </p>
	 * 
	 * @param userName
	 * @param fullname
	 * @param password
	 * @param phoneNo
	 * @param session
	 * @return
	 */

	@RequestMapping(value = "/changeInformation", method = POST)
	public String processedit(@Valid Sender sender, Errors errors, Model model, HttpSession session) {
		if (errors.hasErrors()) {
			return "sender/changeInformation";
		}
		sender = senderRepository.updateUserNameFullNamePasswordPhoneNoById(sender);
		session.setAttribute("sender", sender);
		model.addAttribute("msg", "修改成功！");
		model.addAttribute("sender", sender);

		return "sender/changeInformation";
	}

	/**
	 * return order list not received
	 * 
	 * @return PaginationSupport<DeliveryOrder>
	 *
	 */
	@RequestMapping(value = "/orderList", method = GET)
	public PaginationSupport<DeliveryOrder> showDeliveryOrder(
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpSession session) {
		long senderId = (long) session.getAttribute("senderId");
		DeliveryOrder order = deliveryOrderRepository.findOneBySenderId(senderId);
		session.removeAttribute("flag");
		if (order == null) {
			session.setAttribute("flag", true);
		} else {
			session.setAttribute("flag", false);
		}
		return deliveryOrderRepository.findPage(pageNo, pageSize, senderId, false);
	}

	/**
	 * return order list of some sender
	 * 
	 * @return PaginationSupport<DeliveryOrder>
	 *
	 */
	@RequestMapping(value = "/myOrder", method = GET)
	public PaginationSupport<DeliveryOrder> showMyOrder(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpSession session) {
		long senderId = (long) session.getAttribute("senderId");
		return deliveryOrderRepository.findPage(pageNo, pageSize, senderId, true);
	}

	/**
	 * progress receive a order
	 * 
	 * @return String
	 *
	 */
	@RequestMapping(value = "/receive/{orderId}", method = GET)
	public String receiveOrder(@PathVariable("orderId") String orderId, HttpSession session) {
		long senderId = (long) session.getAttribute("senderId");
		DeliveryOrder ordering = deliveryOrderRepository.findOneBySenderId(senderId);
		session.removeAttribute("flag");
		if (ordering == null) {
			DeliveryOrder order = deliveryOrderRepository.findByOrderId(orderId);
			order.setReceiveTime(new Date());
			order.setSender((Sender) session.getAttribute("sender"));
			order.setState(1);
			deliveryOrderRepository.update(order);
			session.removeAttribute("orderIng");
			DeliveryOrder deliveryOrder = deliveryOrderRepository
					.findOneBySenderId((Long) session.getAttribute("senderId"));
			session.setAttribute("orderIng", deliveryOrder);
			return "redirect:/";
		} else {
			return "redirect:/";
		}
	}

	/**
	 * finish a order
	 * 
	 * @return String
	 *
	 */
	@RequestMapping(value = "/finish/{orderId}", method = GET)
	public String finishOrder(@PathVariable("orderId") String orderId, HttpSession session) {
		DeliveryOrder order = deliveryOrderRepository.findByOrderId(orderId);
		order.setFinishTime(new Date());
		order.setState(2);
		deliveryOrderRepository.update(order);
		session.removeAttribute("orderIng");
		DeliveryOrder deliveryOrder = deliveryOrderRepository
				.findOneBySenderId((Long) session.getAttribute("senderId"));
		session.setAttribute("orderIng", deliveryOrder);
		return "redirect:/";
	}

	@RequestMapping("/getTime")
	public String getTime(Model model, HttpServletRequest request, HttpSession session) throws Exception {
		DeliveryOrder deliveryOrder = deliveryOrderRepository
				.findOneBySenderId((Long) session.getAttribute("senderId"));
		model.addAttribute("receiveTime", deliveryOrder.getReceiveTime());
		return "redirect:/";// 你要跳转的JSP路径
	}

}
